﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Silverlight.Testing;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Numbers;
using Numbers.Arithmatic;

namespace t_Numbers
{
    [TestClass]
    public class t_Division
    {
        [TestMethod]
        public void TestCtor()
        {
            Value r1 = new Value();
            Value n1 = new Value() {Val = 10};
            Value n2 = new Value() { Val = 2 };

            Division.Create(new Operand(r1), new Operand(n1), new Operand(n2));
            Assert.AreEqual(5.0, r1.Val, "Initialization didn't work");
        }

        [TestMethod]
        public void TestDivideByZero()
        {
            Value r1 = new Value();
            Value n1 = new Value() { Val = 10 };
            Value n2 = new Value() { Val = 0.0 };

            Division.Create(new Operand(r1), new Operand(n1), new Operand(n2));
            Assert.AreEqual(0.0, r1.Val, "Initialization didn't work");
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentException))]
        public void TestInitialBadLimits()
        {
            Value r1 = new Value() { MaxValue = 2.0 };
            Value n1 = new Value() { Val = 10 };
            Value n2 = new Value() { Val = 2 };

            Division.Create(new Operand(r1), new Operand(n1), new Operand(n2));
        }

        [TestMethod]
        public void TestNoResultUpdate()
        {
            Value r1 = new Value();
            Value n1 = new Value() { Val = 10 };
            Value n2 = new Value() { Val = 2 };

            Division.Create(new Operand(r1), new Operand(n1), new Operand(n2));
            r1.Val = 3.0;
            Assert.AreEqual(5.0, r1.Val, "Result shouldn't have changed!");
        }

        [TestMethod]
        public void TestRestrictions()
        {
            Value r1 = new Value() { MinValue = 0.0, MaxValue = 3.0 };
            Value n1 = new Value() { Val = 10 };
            Value n2 = new Value() { Val = 5 };

            Division.Create(new Operand(r1), new Operand(n1, true), new Operand(n2, true));

            n2.Val = 2.0;
            Assert.AreEqual(5.0, n2.Val, "The value shouldn't have updated due to constraints");
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentException))]
        public void TestResultCantVary()
        {
            Value r1 = new Value();
            Value n1 = new Value() { Val = 10 };
            Value n2 = new Value() { Val = 2 };

            Division.Create(new Operand(r1, true), new Operand(n1), new Operand(n2));
        }

        [TestMethod]
        public void TestUpdate()
        {
            Value r1 = new Value();
            Value n1 = new Value() { Val = 10 };
            Value n2 = new Value() { Val = 2 };

            Division.Create(new Operand(r1), new Operand(n1), new Operand(n2));

            n1.Val = 20;
            Assert.AreEqual(10.0, r1.Val, "Update of n1 didn't propagate");

            n2.Val = 4.0;
            Assert.AreEqual(5.0, r1.Val, "Update of n2 didn't propagate");
        }
    }
}